import { ref, computed } from 'vue'
export default function useEditTodo(todoRef) {
    const editRef = ref(null); // 当前正在修改的任务
    let originTitle = null
    const editTodo = (todo) => {
        editRef.value = todo
        originTitle = todo.title
    }
    const doneTodo = () => {
        console.log('doneTodo')
        editRef.value = null
    }
    const cancleEdit = () => {
        editRef.value.title = originTitle
        doneTodo()
    }

    const allDoneRef = computed({
        get() {
            return todoRef.value.filter(todo => !todo.completed).length === 0;
        },
        set(val) {
            todoRef.value.forEach(todo => todo.completed = val)
        }
    })

    return {
        allDoneRef,
        editRef,
        editTodo,
        doneTodo,
        cancleEdit
    }
}